package com.lance.modules.front.login.action;

import java.util.UUID;

import com.hazelcast.util.StringUtil;
import com.lance.core.base.BC;
import com.lance.core.base.BaseResful;
import com.lance.core.base.VAutowired;
import com.lance.core.base.VResfulMapping;
import com.lance.core.util.JacksonUtil;
import com.lance.modules.front.login.entity.UserEntity;
import com.lance.modules.front.login.service.UserService;

import io.vertx.core.eventbus.Message;

@VResfulMapping
public class UserResful extends BaseResful {
	
	@VAutowired
	public UserService userService;
	
	@VResfulMapping(path="login")
	public void login(String params,Message<Object> message){
		vertx.executeBlocking(bc->{
			UserEntity userEntity = JacksonUtil.read(params,UserEntity.class);
			UserEntity user = userService.login(userEntity);
			if(null!=user && !StringUtil.isNullOrEmptyAfterTrim(user.getId())){
				String token  = UUID.randomUUID().toString();
				BC.TOKENMAPPER.put(token, user,30*24*60*60*1000 ,comu->{
					if(comu.succeeded()){
						user.setToken(token);
						System.out.println(user.toString());
						userService.save(user);
						success(user,message);
					}else{
						error(comu.cause().getMessage(),message);
					}
				});
			}else{
				error("用户名或密码不正确!",message);
			}
		},false, res->{
			error(res.cause().getMessage(),message);
		});
	}
	
	@VResfulMapping(path="list")
	public void list(String params,Message<Object> message){
		vertx.executeBlocking(bc->{
			success(userService.listUser(),message);
		},false, res->{
			error(res.cause().getMessage(),message);
		});
	}
	
}
